package com.zzh.structuredeployment.frag

import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.zzh.custom.loading.LoadingOperation
import com.zzh.structuredeployment.R
import com.zzh.tools.system.ToolScreen

class LoadingFragment : Fragment() {

    private val mLoadingOperation: LoadingOperation by lazy {
        LoadingOperation(requireView().findViewById<View>(R.id.rootView)).apply {
            showOngoing()
        }
    }

    private val mRecyclerView by lazy {
        requireView().findViewById<RecyclerView>(R.id.recyclerView)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_loading, container, false).apply {
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setClick()
        configRecyclerView()
        mLoadingOperation
    }

    private fun configRecyclerView() {
        mRecyclerView.adapter = createAdapter()
        mRecyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            val mDip2px15 = ToolScreen.dip2px(context, 15f)
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                super.getItemOffsets(outRect, view, parent, state)
                outRect.left = mDip2px15
                outRect.right = mDip2px15
                outRect.top = mDip2px15
            }
        })

    }

    private fun createAdapter(): RecyclerView.Adapter<RecyclerView.ViewHolder> {
        return object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
            val list = arrayListOf<String>().apply {
                add("")
                add("")
                add("")
            }

            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): RecyclerView.ViewHolder {
                return object : RecyclerView.ViewHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_loading_item, parent, false)
                ) {

                }
            }

            override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            }

            override fun getItemCount(): Int = list.size

        }
    }

    private fun setClick() {
        requireView().apply {
            findViewById<View>(R.id.showLoading).setOnClickListener {
                mLoadingOperation.showLoading()
            }
            findViewById<View>(R.id.showEmpty).setOnClickListener {
                mLoadingOperation.showEmpty()
            }
            findViewById<View>(R.id.showError).setOnClickListener {
                mLoadingOperation.showError()
            }
            findViewById<View>(R.id.showSuccess).setOnClickListener {
                mLoadingOperation.showSuccess()
            }
            findViewById<View>(R.id.showOngoing).setOnClickListener {
                mLoadingOperation.showOngoing()
            }
        }
    }
}